package com.whut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.whut.model.Groupmember;
import com.whut.service.GroupService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/team")
public class TeamController {
    /*
        @Author 彭西明
        @Date 2019-05-22
        小组管理模块
     */
    @Resource
    private GroupService groupService;

    @ResponseBody
    @PostMapping("/getAllCandidateUser")
    public Object getAllCandidateUser() {   //所有候选用户，用在新增小组
        return groupService.getAllCandidateUser();//[]
    }

    @ResponseBody
    @PostMapping("/getOtherCandidate")
    public Object getOtherCandidate(int groupId) {//除了本组以外的所有候选人，用来给小组增加成员
        return groupService.getOtherCandidate(groupId);//[]
    }

    @ResponseBody
    @PostMapping("/addTeam")
    public String addTeam(String groupName, String teamMembers) {//添加小组

        //1. 生成小组Id，添加小组表 [groupId,groupName]
        //2. 添加小组成员表   [groupId,userId,memberName,isHead]
//        System.out.println(teamMembers);
        List<Groupmember> list = JSON.parseObject(teamMembers, new TypeReference<ArrayList<Groupmember>>(){});//转换成实体类的List
        return groupService.addTeam(groupName,list);
    }

    @ResponseBody
    @PostMapping("/getGroupByPage")
    public Object getGroupByPage(String searchParam, int pageNum, int pageSize) { //分页查找小组,

        //1. groupName 为空
        if (searchParam.equals("")||searchParam==null){
            return groupService.getGroupByPage(pageNum,pageSize);
        }
        //2. groupName不为空
        return null;//[{组名，组长}]
    }

    @ResponseBody
    @PostMapping("/getGroupMembersByGroupId")
    public Object getGroupMembersByGroupId(int groupId) {//根据小组id查找该组的所有成员
        return groupService.getGroupMembersByGroupId(groupId);//[{姓名，userId}]
    }

    @ResponseBody
    @PostMapping("/deleteGroupById")
    public String deleteGroupById(int groupId) {//删除小组

        //1. 删除小组表项
        //2. 删除小组成员表项
        return groupService.deleteGroupById(groupId);
    }

    @ResponseBody
    @PostMapping("/addGroupMember")
    public String addGroupMember(String members) {//增加小组成员

        //只需要，添加小组成员表的一项
        List<Groupmember> list = JSON.parseObject(members, new TypeReference<ArrayList<Groupmember>>(){});//转换成实体类的List
        return groupService.addGroupMember(list);
    }

    @ResponseBody
    @PostMapping("/deleteGroupMember")
    public String deleteGroupMember(int groupId,int userId){//删除一个小组成员
        return groupService.deleteGroupMember(groupId,userId);
    }


    /*
    李元良
    小组成员查询
     */
    @ResponseBody
    @PostMapping("/getUserByVagueName")
    public Object addGroupMumberByName(String userName){
        return groupService.getUserByVagueName(userName);
    }

    /*设置组长*/
    @ResponseBody
    @PostMapping("/setHeader")
    public Object setHeader(int userId,int groupId) {
        System.out.println("在这里！！！！！！" + userId + groupId);
         return groupService.setHeader(userId,groupId);

    }
    /*取消组长*/
    @ResponseBody
    @PostMapping("/cancelHeader")
    public Object cancelHeader(int userId,int groupId) {
        System.out.println("在这里！！！！！！" + userId + groupId);
        return groupService.cancelHeader(userId,groupId);

    }

    /*获取小组组长个数*/
    @ResponseBody
    @PostMapping("/countHeader")
    public Object countHeader(int groupId){
        return groupService.countHeader(groupId);
    }


    /*废弃小组*/
    @ResponseBody
    @PostMapping("/discardByGroupId")
    public String discardByGroupId(int groupId){
        return groupService.discardByGroupId(groupId);
    }

    @ResponseBody
    @PostMapping("/freeByGroupId")
    public String freeByGroupId(int groupId){
        return groupService.freeByGroupId(groupId);
    }

    /*获取小组信息*/
    @ResponseBody
    @PostMapping("/getGroupAllInfo")
    public Object getGroupInfo(){
        return groupService.getGroupInfo();
    }
}
